#include "SFilterTree.h"

#include <core/ImageMgr.h>
#include <core/StyleMgr.h>
#include <qtooltip.h>

#include <QApplication>
#include <QColor>
#include <QHBoxLayout>
#include <QRect>
#include <QStylePainter>

#include "SImageButton.h"

namespace {
const int gap = 4;
const int buttonHeight = 24;
}  // namespace

////////////////////////////////////////////////////////
//	SFilterTree
////////////////////////////////////////////////////////
SFilterTree::SFilterTree(QWidget* p, bool showClear, bool showInvert,
                         QString strColor, QString strBorderColor,
                         int itemHeight, int dropHeight)
    : QFrame(p), m_nameWidth(0), m_leftOffset(8), m_bSignFromParent(false) {
  m_layout = new QHBoxLayout(this);
  m_tree = new STreeComboBox(this, showInvert, strColor, strBorderColor,
                             itemHeight, dropHeight);
  m_tree->setFixedHeight(buttonHeight);
  m_layout->addWidget(m_tree);

  if (showClear) {
    m_btnClear = new SImageButton(this, "widgets_action_clean");
    m_layout->addWidget(m_btnClear);
    connect(m_btnClear, &SImageButton::clicked, this, &SFilterTree::onBtnClick);
  }

  connect(m_tree, &STreeComboBox::itemSelectChanged, this,
          &SFilterTree::onTreeItemSelectChanged);
}

void SFilterTree::setName(const QString& sName) {
  m_name = sName;
  if (m_name.isEmpty()) return;
  if (!m_layout) return;

  if (m_nameWidth <= 0) {
    QFontMetrics fmName(font());
    m_nameWidth = fmName.horizontalAdvance(m_name) + 16;
  }
  m_layout->setContentsMargins(m_nameWidth + gap, 0, gap, 0);
}

void SFilterTree::setNameWidth(int width) {
  m_nameWidth = width;
  m_layout->setContentsMargins(m_nameWidth + gap, 0, gap, 0);
}

void SFilterTree::setDefaultString(const QString& strDefault) {
  assert(m_tree);
  m_tree->setDefaultString(strDefault);
}

void SFilterTree::setDropFixedWidth(int width) {
  assert(m_tree);
  m_tree->setDropFixedWidth(width);
}

void SFilterTree::setLeftOffset(int left) { m_leftOffset = left; }

void SFilterTree::setTreeData(STreeData& treeData) {
  assert(m_tree);
  m_tree->setTreeData(treeData);
}

void SFilterTree::setTreeEnable(bool enable) {
  m_tree->setTreeEnable(enable);
  setEnabled(enable);
}

void SFilterTree::setComboStyleSheet(const QString& stylesheet) {
  m_tree->setStyleSheet(m_tree->styleSheet() + stylesheet);
}

void SFilterTree::clearSelect() {
  m_bSignFromParent = true;
  if (!m_tree) return;
  m_tree->clearSelect();
}

const QStringList& SFilterTree::getSelectItemData() {
  assert(m_tree);
  return m_tree->getSelectItemData();
}

void SFilterTree::onBtnClick() {
  if (!m_tree) return;
  m_tree->clearSelect();
}

void SFilterTree::onTreeItemSelectChanged() {
  if (!m_bSignFromParent) emit filterChanged();
  m_bSignFromParent = false;
}

void SFilterTree::paintEvent(QPaintEvent* event) {
  if (m_nameWidth <= 0) return;

  QStylePainter painter(this);
  QRect rc(0, 0, m_nameWidth, buttonHeight);
  QColor bkColor = NAME_COLOR("StaticTextBK");
  painter.setPen(Qt::NoPen);
  painter.setBrush(bkColor);
  painter.drawRoundedRect(rc, 2, 2);

  rc.setLeft(rc.left() + m_leftOffset);

  if (isEnabled()) {
    painter.setPen(StyleMgr::instance().color("TitleWord"));
  } else {
    painter.setPen(StyleMgr::instance().color("TipsBorder"));
  }

  painter.drawText(rc, m_name, QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
}